package jgl.vajra.management.manager;

import android.content.Context;
import android.content.Intent;

import androidx.fragment.app.FragmentActivity;

import com.orhanobut.logger.Logger;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import jgl.vajra.management.listener.LoginOrBindListener;
import jgl.vajra.management.widget.utils.CommonUtils;
import jgl.vajra.management.widget.utils.ToastUtils;

public class UMLoginManager {

    String type;
    private int status;
    private int statusWx;
    private int statusWb;
    private LoginOrBindListener listener;
    private boolean isBind;

    public void setPushAlias(long id) {
       /* getPushAgent().setAlias(String.valueOf(id), "userId", new UTrack.ICallBack() {
            @Override
            public void onMessage(boolean b, String s) {

            }
        });*/
    }

    public void removePushAlias(long id) {
      /*  getPushAgent().deleteAlias(String.valueOf(id), "userId", new UTrack.ICallBack() {
            @Override
            public void onMessage(boolean b, String s) {

            }
        });*/
    }

    public void initSDK(Context context) {
        UMConfigure.init(context, "6200e96ce0f9bb492bf6737e"
                , "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");// 5de714cf0cafb25a49000564

        PlatformConfig.setWeixin("wx7404d6b3db61cbfb", "2fa4da5772d8a615ef542a6395ed6bc6");
        //   PlatformConfig.setSinaWeibo("1883019891", "fd97a6071fe62c43b94011fab8a1982d", "https://api.weibo.com/oauth2/default.html");
//        PlatformConfig.setQQZone("101537235", "c8deb8988471c5bc7dfb501eb808f69c");
        PlatformConfig.setAlipay("2021003117611848");
        UMConfigure.setLogEnabled(!CommonUtils.isAppRelease());
    }

   /* public PushAgent getPushAgent() {
        return BaseApplication.getPushAgent();
    }*/

    private static class UMengLoginManagerSingleton {
        private static final UMLoginManager INSTANCE = new UMLoginManager();

    }

    public static UMLoginManager getInstance() {
        return UMengLoginManagerSingleton.INSTANCE;
    }

    UMAuthListener authListener = new UMAuthListener() {
        /**
         * @desc 授权开始的回调
         * @param platform 平台名称
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @desc 授权成功的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param data 用户资料返回
         */
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
            if (!isBind) return;
            Logger.d("onComplete=======> ...."+data);
            switch (platform) {
                case QQ:
                    type = "qq_app";
                    break;
                case WEIXIN:
                    type = "wx_app";
                    break;
                case SINA:
                    type = "wb_app";
                    break;
            }
            if (listener != null)
                listener.onSuccess(data, type);
            /*if (status == 1){
                listener.onSuccess(data,status);
            }else{
                listener.onSuccess(data,status);
            }*/


//            Toast.makeText(mContext, "成功了", Toast.LENGTH_LONG).show();
        }

        /**
         * @desc 授权失败的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            ToastUtils.showShort("失败:" + t.getMessage());
            listener.failure();
//            Toast.makeText(mContext, "失败：" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @desc 授权取消的回调
         * @param platform 平台名称
         * @param action 行为序号，开发者用不上
         */
        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
//            Toast.makeText(mContext, "取消了", Toast.LENGTH_LONG).show();
           // ToastUtils.showShort("取消了");
            listener.failure();
        }
    };

    public void setLoginOrBindListener(LoginOrBindListener listener) {
        this.listener = listener;
    }

    /*public interface LoginOrBindListener{
        void onSuccess(String uid,String type);
        void failure();
    }

    LoginOrBindListener loginOrBindListener;
    public void onLoginOrBindListener(LoginOrBindListener loginOrBindListener){
        this.loginOrBindListener = loginOrBindListener;
    }*/

    public void onActivityResult(int requestCode, int resultCode, Intent data, FragmentActivity activity) {
        if (data == null) return;
        UMShareAPI.get(activity).onActivityResult(requestCode, resultCode, data);
    }

    public void umengLoginWx(FragmentActivity activity) {
        loginWx(activity);
    }

    public void umengDeleteOauthWx(FragmentActivity activity) {
        deleteOauthWx(activity);
    }

    public void umengLoginQQ(FragmentActivity activity) {
        loginQQ(activity);
    }

    public void umengDeleteOauthQQ(FragmentActivity activity) {
        deleteOauthQQ(activity);
    }

    public void umengLoginWeibo(FragmentActivity activity) {
        loginWeibo(activity);
    }

    public void umengDeleteOauthWeibo(FragmentActivity activity) {
        deleteOauthWeibo(activity);
    }

    public void loginWx(FragmentActivity activity) {
        isBind = true;
        UMShareAPI.get(activity).getPlatformInfo(activity, SHARE_MEDIA.WEIXIN, authListener);
    }

    private void deleteOauthWx(FragmentActivity activity) {
        isBind = false;
        UMShareAPI.get(activity).deleteOauth(activity, SHARE_MEDIA.WEIXIN,null);
    }

    private void loginQQ(FragmentActivity activity) {
        isBind = true;
        UMShareAPI.get(activity).getPlatformInfo(activity, SHARE_MEDIA.QQ, authListener);
    }

    private void deleteOauthQQ(FragmentActivity activity) {
        UMShareAPI.get(activity).deleteOauth(activity, SHARE_MEDIA.QQ, authListener);
    }

    private void loginWeibo(FragmentActivity activity) {
        UMShareAPI.get(activity).getPlatformInfo(activity, SHARE_MEDIA.SINA, authListener);
    }

    private void deleteOauthWeibo(FragmentActivity activity) {
        UMShareAPI.get(activity).deleteOauth(activity, SHARE_MEDIA.SINA, authListener);
    }

    public void loginAlipay(FragmentActivity activity) {
        UMShareAPI.get(activity).getPlatformInfo(activity, SHARE_MEDIA.ALIPAY, authListener);
    }

    public void deleteOauthAlipay(FragmentActivity activity) {
        UMShareAPI.get(activity).deleteOauth(activity, SHARE_MEDIA.ALIPAY, authListener);
    }

}
